﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP09S03_OfficeTracking
{
    public partial class FormLogin : Form
    {
        public FormLogin()
        {
            InitializeComponent();
        }

        int retry_count = 0;
        private void btnLogin_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            toolStripProgressBar1.Visible = true;

            if (controlUserID.UserText.Trim() == "") {

                errorProvider1.SetError(controlUserID.TextBox, "please enter user name!");
                controlUserID.TextBox.Focus();
                return;
            }

            if (controlPassword.UserText.Trim() == "") {
                errorProvider1.SetError(controlPassword.TextBox, "please enter password!");
                controlPassword.TextBox.Focus();
                return;
            
            }

            toolStripProgressBar1.PerformStep();
            
            try
            {
                Controller.EmployeController ec = new Controller.EmployeController();

                if (ec.ValidateLogin(controlUserID.UserText, controlPassword.UserText))
                {
                    MainForm mf = new MainForm(controlUserID.UserText);

                    mf.Show();

                    this.Hide();
                }
                else
                {

                    retry_count = retry_count + 1;

                    lblMessage.Text = "Invalid User Name or Password.Attempt Count: " + Convert.ToString(retry_count);

                    if (retry_count > 5) {
                        MessageBox.Show("You already tried the maximun allowed times.Please try it later");
                        btnLogin.Enabled = false;
                        btnReset.Enabled = false;

                    
                    }
                }

            }
            catch (Exception exc)
            {
                lblMessage.Text = exc.Message;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            controlUserID.UserText = "";
            controlPassword.UserText = "";
            lblMessage.Text = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


    }
}
